package cc.jq1024.domain.credit.service;

import cc.jq1024.domain.credit.event.CreditAdjustSuccessMessageEvent;
import cc.jq1024.domain.credit.model.aggregate.TradeAggregate;
import cc.jq1024.domain.credit.model.entity.CreditAccountEntity;
import cc.jq1024.domain.credit.model.entity.CreditOrderEntity;
import cc.jq1024.domain.credit.model.entity.CreditTaskEntity;
import cc.jq1024.domain.credit.model.entity.TradeEntity;
import cc.jq1024.domain.credit.repository.ICreditRepository;
import cc.jq1024.types.event.BaseEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 积分调额服务【正逆向，增减积分】
 *
 * @author li--jiaqiang
 * @date 2024−07−16
 */
@Slf4j
@Service
public class CreditAdjustService implements ICreditAdjustService {

    @Resource
    private ICreditRepository creditRepository;
    @Resource
    private CreditAdjustSuccessMessageEvent creditAdjustSuccessMessageEvent;

    /**
     * 增加账户积分额度（行为奖励配置 - 积分奖励）
     *
     * @param tradeEntity 下单实体
     * @return 订单号
     */
    @Override
    public String createOrder(TradeEntity tradeEntity) {
        log.info("增加账户积分额度开始 userId:{} tradeName:{} amount:{}", tradeEntity.getUserId(), tradeEntity.getTradeName(), tradeEntity.getAmount());
        // 1. 创建账户积分实体
        CreditAccountEntity creditAccountEntity = TradeAggregate.createCreditAccountEntity(
                tradeEntity.getUserId(),
                tradeEntity.getAmount());

        // 2. 创建账户订单实体
        CreditOrderEntity creditOrderEntity = TradeAggregate.createCreditOrderEntity(
                tradeEntity.getUserId(),
                tradeEntity.getTradeName(),
                tradeEntity.getTradeType(),
                tradeEntity.getAmount(),
                tradeEntity.getOutBusinessNo());

        // 3. 创建任务实体
        CreditAdjustSuccessMessageEvent.CreditAdjustSuccessMessage creditAdjustSuccessMessage =
                CreditAdjustSuccessMessageEvent.CreditAdjustSuccessMessage.builder()
                        .userId(tradeEntity.getUserId())
                        .orderId(creditOrderEntity.getOrderId())
                        .amount(tradeEntity.getAmount())
                        .outBusinessNo(tradeEntity.getOutBusinessNo())
                        .build();
        BaseEvent.EventMessage<CreditAdjustSuccessMessageEvent.CreditAdjustSuccessMessage>
                creditAdjustSuccessMessageEventMessage = creditAdjustSuccessMessageEvent.buildEventMessage(creditAdjustSuccessMessage);
        CreditTaskEntity taskEntity = TradeAggregate.buildCreditTaskEntity(tradeEntity.getUserId(), creditAdjustSuccessMessageEvent.topic(), creditAdjustSuccessMessageEventMessage.getId(), creditAdjustSuccessMessageEventMessage);

        // 4. 构建交易聚合对象
        TradeAggregate tradeAggregate = TradeAggregate.builder()
                .userId(tradeEntity.getUserId())
                .creditAccountEntity(creditAccountEntity)
                .creditOrderEntity(creditOrderEntity)
                .taskEntity(taskEntity)
                .build();

        // 5. 保存积分交易订单
        creditRepository.saveUserCreditTradeOrder(tradeAggregate);
        log.info("增加账户积分额度完成 userId:{} orderId:{}", tradeEntity.getUserId(), creditOrderEntity.getOrderId());

        return creditOrderEntity.getOrderId();
    }

    /**
     * 查询用户积分账户余额
     *
     * @param userId 用户ID
     * @return 返回结果
     */
    @Override
    public CreditAccountEntity queryUserCreditAccount(String userId) {
        return creditRepository.queryUserCreditAccount(userId);
    }
}